<template>
  <div class="bread-crumb">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        v-for="bread in breadList"
        :key="bread.name"
        @click.native="changeRouter(bread)"
        >{{ bread.label }}</el-breadcrumb-item
      >
    </el-breadcrumb>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
interface BreadType {
  label: string;
  name: string;
}
export default Vue.extend({
  name: 'bread-crumb',
  data() {
    return {
      breadList: [] as BreadType[]
    }
  },
  methods: {
    changeRouter(bread: BreadType) {
      if (this.$route.name === bread.name) return false
      this.$router.push({
        name: bread.name
      })
    }
  },
  watch: {
    $route: {
      immediate: true,
      handler(route: any) {
        this.breadList = []
        route.matched.forEach((data: any) => {
          if (data.meta.breadName) {
            const bread = {
              label: data.meta.breadName,
              name: data.meta.name || data.name
            }
            this.breadList.push(bread)
          }
        })
      }
    }
  }
})
</script>
<style lang="scss" scoped>
::v-deep .el-breadcrumb__inner {
  cursor: pointer;
  &:hover{
    color: cornflowerblue;
  }
}
</style>
